package com.example.administrator.myapplication.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.example.administrator.myapplication.utils.SharedPreFerencesTools;

public class MainActivity extends AppCompatActivity {

    private LocationClient mLocClient; //声明 地图定位客户端
    public MyLocationListenner myListener = new MyLocationListenner(); //声明sdk监听函数
    boolean isFirstLoc = true;// 是否首次定位

    double Latitude;//纬度
    double Longitude;//经度
    String Locationname; // 所在位置

    private int temp=0;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            if (msg.what == 0) {
                temp++;
                if (temp==1){
                    startActivity(new Intent(MainActivity.this,HomeActivity.class));
                }
            }
            if (msg.what == 1) {

            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iniData();
    }

    private void iniData() {
        // 定位初始化
        mLocClient = new LocationClient(this);
        mLocClient.registerLocationListener(myListener);  //注册定位监听函数
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(com.baidu.location.LocationClientOption.LocationMode.Hight_Accuracy);
        option.setOpenGps(true);// 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
        option.setIsNeedAddress(true);
        option.setNeedDeviceDirect(true);
        mLocClient.setLocOption(option);//设置 LocationClientOption
        mLocClient.start();
    }


    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null )
                return;
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                            // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(location.getDirection())//
                    .latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            System.out.println("经度信息" + location.getLongitude());
            System.out.println("纬度信息" + location.getLatitude());
            System.out.println("详细" + location.getAddrStr());
            System.out.println("方位" + location.getDirection());

            Latitude = location.getLatitude();
            Longitude = location.getLongitude();
            Locationname = location.getAddrStr();
            SharedPreFerencesTools.setUserAddress(Locationname);
            Message ms = handler.obtainMessage(temp, Locationname);
            handler.sendMessage(ms);
            if (isFirstLoc) {
                isFirstLoc = false;
                LatLng ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
            }
        }

        public void onReceivePoi(BDLocation poiLocation) {

        }
    }

}
